﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using CPETwit.WPFClient.Business.Service;
using CPETwit.WPFClient.Business.TwitterServiceReference;

namespace CPETwit.WPFClient.Common
{
    public static class ServiceManager
    {
        private static WCFServiceCaller SERVICE_CALLER = CommonData.GetServiceCaller();

        public static User RefreshUser(User user)
        {
            return SERVICE_CALLER.GetUserByLogin(user.login);
        }

        public static void RefreshCurrentUser()
        {
            CommonData.LOGGED_USER = RefreshUser(CommonData.LOGGED_USER);
            DialogManager.UpdateTweetMenus();
        }

        public static int GetTweetCount(User user)
        {
            return SERVICE_CALLER == null ?
                0 : GetTweetCountByRequest(user);
        }

        private static int GetTweetCountByRequest(User user)
        {
            IEnumerable<Tweet> tweets = GetTweetsFromUser(user);
            if (tweets != null)
                return tweets.Count();
            return 0;
        }



        public static void ManageFollow(User user)
        {
            if (CommonData.IsFollowed(CommonData.LOGGED_USER, user) == false)
                SERVICE_CALLER.Follow(CommonData.LOGGED_USER, user);
            else
                SERVICE_CALLER.UnFollow(CommonData.LOGGED_USER, user);
        }

        public static IEnumerable<User> GetUsersFromResearch(string research)
        {
            if (SERVICE_CALLER != null)
                return SERVICE_CALLER.GetUsersByText(research);
            return null;
        }

        public static IEnumerable<Tweet> GetTimeLineFromUser(User user)
        {
            if (SERVICE_CALLER == null)
                return null;
            return SERVICE_CALLER.GetUserTimeline(user);
        }

        public static IEnumerable<Tweet> GetTweetsFromUser(User user)
        {
            return SERVICE_CALLER.GetUserTweets(user);
        }

        public static void ReTweet(User user, Tweet tweet)
        {
            bool hasRetweet = CommonData.HasReTweeted(user, tweet);
            if (hasRetweet)
                SERVICE_CALLER.UndoReTweet(user, tweet);
            else
                SERVICE_CALLER.ReTweet(user, tweet, false);
        }

        public static IEnumerable<Tweet> GetTweetsBySearch(string research)
        {
            return SERVICE_CALLER.GetTweetsByResearch(research);
        }

        public static IEnumerable<Tweet> GetTweetsByTag(string search)
        {
            return SERVICE_CALLER.GetTweetsByTag(search);
        }
    }
}
